#include "stdio.h"
#include "SDL/SDL.h"
#include "newgame.h"
int main(int argc, char *argv[]) {
SDL_Surface *screen; //This pointer will reference the backbuffer
SDL_Surface *image; //This pointer will reference our bitmap sprite
SDL_Surface *bg;//This pointer will temporarily reference our bitmap sprite
SDL_Surface *help;//This pointer will temporarily reference our bitmap sprite


SDL_Event event;

SDL_Surface *select; //This pointer will reference our bitmap sprite

SDL_Surface *slc;//This pointer will temporarily reference our bitmap sprite
SDL_Surface *hlp;//This pointer will temporarily reference our bitmap sprite


SDL_Rect src, dest;//These rectangles will describe the source and destination regions of our blit
SDL_Rect srcl, destl;//These rectangles will describe the source and destination regions of our blit
SDL_Rect srch, desth;//These rectangles will describe the source and destination regions of our blit


//We must first initialize the SDL video component, and check for success
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}
//Set the video mode to fullscreen 640x480 with 16bit colour and double- buffering
screen = SDL_SetVideoMode(945, 588, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
if (screen == NULL) {
printf("Unable to set video mode: %s\n", SDL_GetError());
return 1;
}
//Load the bitmap into a temporary surface, and check for success
bg = SDL_LoadBMP("img/screen ttt 2.bmp");
slc = SDL_LoadBMP("img/selects.png");
hlp = SDL_LoadBMP("img/screen ttt help.bmp");

if (bg == NULL) {
printf("Unable to load bitmap: %s\n", SDL_GetError());
return 1;
}
//Convert the surface to the appropriate display format
image = SDL_DisplayFormat(bg);
select = SDL_DisplayFormat(slc);
help = SDL_DisplayFormat(hlp);

SDL_SetColorKey( select, SDL_SRCCOLORKEY, SDL_MapRGB(select->format, 255, 0, 0) );
//Release the temporary surface
SDL_FreeSurface(bg);
//Construct the source rectangle for our blit
src.x = 0;
src.y = 0;
src.w = image->w; //Use image->w to display the entire width of the image
src.h = image->h; //Use image->h to display the entire height of the image
//Construct the destination rectangle for our blit
dest.x = 0;//Display the image at the (X,Y) coordinates (100,100)
dest.y = 0;
dest.w = image->w;
dest.h = image->h;

//Release the temporary surface
SDL_FreeSurface(slc);
//Construct the source rectangle for our blit
srcl.x = 0;
srcl.y = 0;
srcl.w = select->w; //Use image->w to display the entire width of the image
srcl.h = select->h; //Use image->h to display the entire height of the image
//Construct the destination rectangle for our blit
destl.x = 488;//Display the image at the (X,Y) coordinates (100,100)
destl.y = 220;
destl.w = select->w;
destl.h = select->h;

//Release the temporary surface
SDL_FreeSurface(hlp);
//Construct the source rectangle for our blit
srch.x = 0;
srch.y = 0;
srch.w = help->w; //Use image->w to display the entire width of the image
srch.h = help->h; //Use image->h to display the entire height of the image
//Construct the destination rectangle for our blit
desth.x = 100;//Display the image at the (X,Y) coordinates (100,100)
desth.y = 100;
desth.w = help->w;
desth.h = help->h;


//Blit the image to the backbuffer
SDL_BlitSurface(image, &src, screen, &dest);
SDL_BlitSurface(select, &srcl, screen, &destl);



int done = 1;
while(done)
{
//Flip the backbuffer to the primary Hardware Video Memory
SDL_Flip(screen);

while (SDL_PollEvent(&event))
{
// check for messages
switch (event.type)
{
// exit if the window is closed
case SDL_QUIT:
done = 0;
break;
// check for keypresses
case SDL_KEYDOWN:
{
// exit if ESCAPE is pressed
if (event.key.keysym.sym == SDLK_ESCAPE)
{done = 0;}
else if (event.key.keysym.sym == SDLK_DOWN)
{
if(destl.y<400)
{destl.y = destl.y + 45;
SDL_BlitSurface(image, &src, screen, &dest);

SDL_BlitSurface(select, &srcl, screen, &destl);
}
    ;}
else if (event.key.keysym.sym == SDLK_UP)
{//Construct the destination rectangle for our blit
if(destl.y>220)
{destl.y = destl.y - 45;
SDL_BlitSurface(image, &src, screen, &dest);

SDL_BlitSurface(select, &srcl, screen, &destl);
}
    ;}
else if ((event.key.keysym.sym == SDLK_KP_ENTER)||(event.key.keysym.sym == SDLK_RETURN))
{
    //Construct the destination rectangle for our blit
if(destl.y==400)
{done=0;}
else if (destl.y==355)
{

SDL_BlitSurface(help, &srch, screen, &desth);

}
else if (destl.y==265)
{
newgame(screen);

SDL_BlitSurface(image, &src, screen, &dest);
SDL_BlitSurface(select, &srcl, screen, &destl);

}
    ;}
break;
}
} // end switch
} // end of message processing

}
//Release the surface
SDL_FreeSurface(image);
SDL_FreeSurface(select);
SDL_FreeSurface(help);

return 0; //Return success!
}
